#include "GfxDriver/WGL/WGLRenderedTexture.h"

#include "GfxDriver/WGL/WGLHelper.h"

#include "GfxDriver/GfxDriver.h"
#include "Core/LogService.h"

namespace Orca {

	//--------------------------------------------------------------------------
	WGLRenderedTexture* WGLRenderedTexture::create(
		 GfxDriver *gfxDriver
		,uint32 w
		,uint32 h
		,PixelFormat format) {

		WGLRenderedTexture *tex = new WGLRenderedTexture;

		if( !tex->_init(gfxDriver, w, h, format) ) {

			delete tex;
			return NULL;
		}

		LOG_MSG(
			"WGLGfxDriver: Textrue " + 
			toStr(w) + " x " +
			toStr(h) + " ;" +
			"glHandle=" + toStr((uint32)tex->mGLHandle) );

		return tex;
	}

	//--------------------------------------------------------------------------
	bool WGLRenderedTexture::_init(
		 GfxDriver *gfxDriver
		,uint32 w
		,uint32 h
		,PixelFormat format) {

		mGfxDriver = gfxDriver;
		mW = w;
		mH = h;
		mFmt = format;

		mGfxDriver->resetErrors();

		glGenTextures(1, &mGLHandle);

		// init gl texture settings
		glBindTexture(GL_TEXTURE_2D, mGLHandle);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

		glGenerateMipmapEXT(GL_TEXTURE_2D);
		
		GLint internalFmt;
		GLenum srcFmt, srcType;
		WGLHelper::getGLPixelFormat(mFmt, internalFmt, srcFmt, srcType);

		glTexImage2D(
			GL_TEXTURE_2D,
			0,
			internalFmt,
			(GLsizei)mW,
			(GLsizei)mH,
			0,
			srcFmt,
			srcType,
			NULL);

		GLenum err = glGetError();

		glBindTexture(GL_TEXTURE_2D, 0);

		if(err != GL_NO_ERROR) {
			return false;
		}

		return true;
	}

	//--------------------------------------------------------------------------
	WGLRenderedTexture::WGLRenderedTexture()
		: mGfxDriver(NULL)
		, mW(0)
		, mH(0)
		, mFmt(PF_UNKNOWN)
		, mGLHandle(0) {

	}
	
	//--------------------------------------------------------------------------
	WGLRenderedTexture::~WGLRenderedTexture() {

		if(mGLHandle)
			glDeleteTextures(1, &mGLHandle);
	}

	//--------------------------------------------------------------------------
	uint32 WGLRenderedTexture::getWidth() const {
		return mW;
	}

	//--------------------------------------------------------------------------
	uint32 WGLRenderedTexture::getHeight() const {
		return mH;
	}

	//--------------------------------------------------------------------------
	PixelFormat WGLRenderedTexture::getPixelFormat() const {
		return mFmt;
	}
	
	//--------------------------------------------------------------------------
	bool WGLRenderedTexture::uploadToGPU(bool autoGenMipmap) {

		return true;
	}

	//--------------------------------------------------------------------------
	unsigned int WGLRenderedTexture::getAPIHandle() const {
		return mGLHandle;
	}

	//--------------------------------------------------------------------------
	unsigned int WGLRenderedTexture::getAPITexType() const {
		return GL_TEXTURE_2D;
	}

	//--------------------------------------------------------------------------

}